package BalanceView;

use strict;
use CGI qw(:standard);

use Athena::WebAPI::HTML;
use Athena::WebAPI::Form;
use Athena::WebAPI::Table;
use Athena::WebAPI::View;

use Athena::NavHeaderView;
use Athena::CellFormat qw(:all);

use base ("View");

sub new {

    # Get the class name
    my $stInvocant = shift(@_);
    my $stClass = ref($stInvocant) || $stInvocant;

    # Create the current instance and bless into class
    my $rSelf = { };
    bless($rSelf,$stClass);

    my %hParams = ( @_ );
    $rSelf->{params} = \%hParams;

    return $rSelf;
}

sub draw {
  my $rSelf = shift(@_);
  my $rParams = $rSelf->{params};

  my $rTable = Table->new(%{$rSelf->{params}->{table_style}});
  $rSelf->buildTable($rTable);

  my $rForm = Form->new(-method=>'post', -name=>'myForm');
  $rForm->addChild($rTable);
  $rForm->addHiddenFields($rParams->{'hidden_fields'});

  my $rHTML = HTML->new();
  $rHTML->setBodyAttr(%{$rSelf->{params}->{body_style}});
  $rHTML->addChild($rForm);
  
  return $rHTML->draw();
}

sub buildTable {
   my $rSelf = shift(@_);
   my $rTable = shift(@_);
   my $iCurRow = 0;
   $rTable->addChild($iCurRow++, 0, NavHeaderView->new($rSelf->{params}));
   $rTable->addChild($iCurRow++, 0, "Balance Request",-bgcolor=>"#ccccdd");
   my $rBalTable = Table->new(-border=>1, -cellpadding=>3);
   
   my $stFarm;
   my $iBCurRow = 0;
   foreach $stFarm (sort(keys(%{$rSelf->{params}->{farms_bal}}))) {
     $iBCurRow++;
     my $stRowColor = $rSelf->cellColor();
     $rBalTable->addChild($iBCurRow, 0, " ".$stFarm." ", 
			  -bgcolor=>$stRowColor);
     $rBalTable->addChild($iBCurRow, 1, 
			  " ". $rSelf->{params}->{farms_bal}->{$stFarm} . " ",
			  -bgcolor=>$stRowColor);
   }

   $rTable->addChild($iCurRow++, 0, $rBalTable);
   $rTable->addChild($iCurRow++, 0, submit(-name=>'send'), 
		     -bgcolor=>'#ddcccc');
   $rTable->addChild($iCurRow++, 0, NavFooterView->new($rSelf->{params}));
}

1;
